//
//  UILabelViewController.swift
//  swift-practice
//
//  Created by 余振涛 on 2021/7/23.
//

import UIKit
import Masonry

class UILabelViewController: UIViewController {

    init(withTitle title:String){
        super.init(nibName: nil, bundle: nil)
        self.title = title
        self.view.backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createLable()
        // Do any additional setup after loading the view.
    }
    
    
    lazy var aLabel : UILabel = {
        let label = UILabel.init()
        label.text = "这是第一个标签"
        label.numberOfLines = 0
        label.textAlignment = .center
        label.backgroundColor = .gray
        return label
    }()
    
    lazy var bLabel : UILabel = {
        let label = UILabel.init()
        return label
    }()
    
    lazy var hotLabel : UILabel = {
        let hot = UILabel.init()
        hot.layer.cornerRadius = 3
        hot.backgroundColor = .yellow
        hot.text = "HOT"
        return hot
    }()
    
    func createLable() {
        
        let atst = NSMutableAttributedString.init(string: "HOT我是富文本！我是富文本！")
        atst.addAttribute(kCTForegroundColorAttributeName as NSAttributedString.Key, value: UIColor.red, range: NSMakeRange(0, 3))
        atst.addAttribute(kCTFontAttributeName as NSAttributedString.Key, value: UIFont.systemFont(ofSize: 9), range: NSMakeRange(0, 3))
        atst.addAttribute(kCTBackgroundColorAttributeName as NSAttributedString.Key, value: UIColor.yellow, range: NSMakeRange(0, 3))
        
        let attachment = NSTextAttachment()
        attachment.bounds = CGRect.init(x: 0, y: 0, width: 25, height: 13)
        let img = UIImage(named: "")
        attachment.image = img
        let string = NSAttributedString.init(attachment: attachment)
        atst.append(string)
        
        
        atst.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: 20), range: NSMakeRange(atst.length / 3 * 2, atst.length / 3))
        bLabel.attributedText = atst
        bLabel.textAlignment = .center
        
        self.view.addSubview(aLabel)
        self.view.addSubview(bLabel)
        
        aLabel.mas_makeConstraints { (make : MASConstraintMaker!) in
            make.top.equalTo()(self.view)?.offset()(-200)
            make.left.equalTo()(self.view)?.offset()(100)
            make.right.equalTo()(self.view)?.offset()(-100)
            make.bottom.equalTo()(self.view)?.offset()(-100)
        }
        
        bLabel.mas_makeConstraints { (make : MASConstraintMaker!) in
            make.left.right().equalTo()(self.view)
            make.bottom.equalTo()(self.view)?.offset()(-(UIScreen.main.bounds.size.height / 2))
        }
    
    }

}
